//
//  LoginViewController.m
//  CommentPage
//
//  Created by 欧杨 on 2020/12/3.
//

#import "LoginViewController.h"
#import <AFNetworking.h>
#import <Masonry.h>
#import "AppDelegate.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property(strong, nonatomic) UITextField *email;
@property(strong, nonatomic) UITextField *password;
@property(strong, nonatomic) UIButton *login;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupSubviews];
}

- (void)setupSubviews
{
    _email = [[UITextField alloc] init];
    _email.delegate = self;
    
    _email.backgroundColor = [UIColor whiteColor];
    _email.clearButtonMode = UITextFieldViewModeWhileEditing;
    //_email.keyboardType = UIKeyboardTypeEmailAddress;
    _email.borderStyle = UITextBorderStyleRoundedRect;
    _email.textColor = [UIColor blackColor];
    
    [_email setPlaceholder:@" 请输入您的邮箱"];
    [_email setText:@"chenhh53@mail2.sysu.edu.cn"];
    [self.view addSubview:_email];
    
    _password = [[UITextField alloc] init];
    _password.delegate = self;
    
    _password.backgroundColor = [UIColor whiteColor];
    _password.clearButtonMode = UITextFieldViewModeWhileEditing;
    _password.borderStyle = UITextBorderStyleRoundedRect;
    _password.textColor = [UIColor blackColor];
    
    [_password setPlaceholder:@" 请输入您的密码"];
    [_password setText:@"00000000"];
    [_password isSecureTextEntry];
    [self.view addSubview:_password];
    
    _login = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [_login.layer setMasksToBounds:YES];
    [_login.layer setCornerRadius:10.0];
    [_login.layer setBorderWidth:1.2];
    _login.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    _login.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _login.backgroundColor = [UIColor colorWithRed:28 green:134 blue:238 alpha:1.0];
    
    
    [_login setTitle:@" 登陆 " forState:UIControlStateNormal];
    [_login addTarget:self action:@selector(signupClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_login];
    
   
    [_email mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@50);
        make.top.equalTo(self.view).offset(200);
        make.left.equalTo(self.view).offset(50);
        make.right.equalTo(self.view).offset(-50);
    }];
    [_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@50);
        make.top.equalTo(_email.mas_bottom).offset(20);
        make.left.equalTo(self.view).offset(50);
        make.right.equalTo(self.view).offset(-50);
    }];
    [_login mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(100, 50));
        make.top.equalTo(_password.mas_bottom).offset(50);
        make.centerX.equalTo(self.view);
    }];
}

#pragma mark -BUTTON

- (void)signupClicked
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    [myDelegate Login:self.email.text password:self.password.text];
    
    /*
    NSDictionary *postParam = @{@"name":_email.text,@"password":_password.text};
    NSString *URLString = [NSString stringWithFormat:@"%@", @"http://172.18.178.56/api/user/login/pass"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:URLString parameters:postParam progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"State"] isEqual:@"success"]) {
            NSLog(@"登陆成功 %@",responseObject);
        } else {
            [self showError:[NSString stringWithFormat:@"%@", responseObject]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请检查网络环境");
    }];
    */
}

- (void)showError:(NSString *)errorMsg {
    // 弹框提醒
    // 初始化对话框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:errorMsg preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    // 弹出对话框
    [self presentViewController:alert animated:true completion:nil];
}

#pragma mark -KEYBOARD

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_email resignFirstResponder];
    [_password resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end
